//
//  WelcomeView.swift
//  微博Swift
//
//  Created by  justinchou on 16/2/26.
//  Copyright © 2016年  justinchou. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeView: UIView {
    
    
    // MARK:- 构造函数
    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
        
        backgroundColor = UIColor.whiteColor()
        print("欢迎界面")
        
        setupUI()
        
    }
    // MARK:- 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //实现动画
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(self).offset(-20)
        }
        // Effect 1
//        UIView.animateWithDuration(2.0,
//            delay: 0.0,
//            usingSpringWithDamping: 0.8,
//            initialSpringVelocity: 10,
//            options: [],
//            animations: { () -> Void in
//                self.layoutIfNeeded()
//            }) { (_) -> Void in
//                self.removeFromSuperview()
//        }
        // Effect 2
        //效果说明 弹力系数为0.8
        UIView.animateWithDuration(2.0,
            delay: 0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                self.layoutIfNeeded()
            }) { _ in
                self.launchImageView.hidden = false
                
                UIView.animateWithDuration(
                    1.0,
                    animations: { () -> Void in
                        self.launchImageView.transform = CGAffineTransformMakeScale(2.0, 2.0)
                        self.alpha = 0
                        self.launchImageView.alpha = 0
                    }) { _ in
                        self.removeFromSuperview()
                }
        }
        
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        removeFromSuperview()
    }
    
    // MARK:- 懒加载控件
    /// 头像图片
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 设置标签
    private lazy var welcomeLabel: UILabel = UILabel(title: "欢迎回来", color: UIColor.orangeColor(), fontSize: 17)
    // MARK: - 背景图片
//    private lazy var backgroundView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var launchImageView = UIImageView(image: NSBundle.justin_launchImageView)
}

// MARK:- 设置界面
extension WelcomeView {
    
    // MARK:- 设置控件
    private func setupUI() {
        
        addSubview(launchImageView)
        
        addSubview(iconView)
        addSubview(welcomeLabel)
        
        let iconWH: CGFloat = 45;
        iconView.layer.cornerRadius = iconWH
        iconView.layer.masksToBounds = true
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(180)
            make.size.equalTo(CGSize(width: iconWH * 2, height: iconWH * 2))
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(20)
        }
        
        launchImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        layoutIfNeeded()
        
        let url = NSURL(string: UserAccount.sharedUserAccount.avatar_large ?? "")
        iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
}

